package com.googlecode.recycled.lang.jse.lang;

import org.apache.commons.lang.UnhandledException;

/**
 * Utility returning information regarding a current stacktrace.
 *
 * @author Henryk Konsek
 */
public class Stacktraces {

    // CONSTANTS

    private static final int CURRENT_CALLER_STACK_LEVEL = 2;

    // CONSTRUCTION

    protected Stacktraces() {
    }

    // UTILITY METHODS

    public static String currentMethodName() {
        return Thread.currentThread().getStackTrace()[CURRENT_CALLER_STACK_LEVEL].getMethodName();
    }

    public static String previousMethodName() {
        return Thread.currentThread().getStackTrace()[CURRENT_CALLER_STACK_LEVEL + 1].getMethodName();
    }

    public static Class<?> currentClassName() {
        try {
            return Class.forName(Thread.currentThread().getStackTrace()[CURRENT_CALLER_STACK_LEVEL].getClassName());
        } catch (ClassNotFoundException e) {
            throw new UnhandledException(e);
        }
    }

    public static Class<?> previousClassName() {
        try {
            return Class.forName(Thread.currentThread().getStackTrace()[CURRENT_CALLER_STACK_LEVEL + 1].getClassName());
        } catch (ClassNotFoundException e) {
            throw new UnhandledException(e);
        }
    }

}
